from myelin.Ns import MYELINS, XML_NS
from nodetypes.ntypes.Document.view.MML import Document, Transform

class Document(Document):
    
    def createHead(self, context, node, locale):
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        nxa(
            nxc(
                u'a:title', MYELINS,
                attributes={
                    u'key' : unicode(context.action),
                    (u'xml:lang',XML_NS): locale,
                },
                content=context.model.rdf_join('dcTitle')
            )
        )
        
        self.createAccount(
            context,
            nxa(
                nxc(
                    u'a:account', MYELINS
                )
            ),
            locale
        )
        
        self.createMeta(
            context,
            nxa(
                nxc(
                    u'a:meta', MYELINS,
                    attributes = {
                        (u'xml:lang',XML_NS) : locale,
                    }
                )
            ),
            locale
        )
        
        nxa(
            nxc(
                u'a:nav', MYELINS
            )
        )
        
        return node
    
                  
    def createContent(self, context, node, locale):
        curNode = node.xml_append(
            node.xml_create_element(
                u'a:grouping', MYELINS,
                attributes={
                    u'role' : u'body'
                }
            )
        )
        
        body = unicode(
            context.source.getContent()
        )
        try:            
            body = body % context.request.params
        except:
            pass 
        
        rdf_join = context.model.rdf_join
        curNode.xml_append(
            curNode.xml_create_element(
                u'a:literal', MYELINS,
                attributes = {
                    u'format' : rdf_join('dcFormat',u'|'),
                    (u'xml:lang', XML_NS) : rdf_join('dcLanguage',u'|'),
                },
                content = body
            )
        )
        
        return node


class Transform(Transform):
    
    newDocument = Document